using System.Reflection;
using System.Text;
using BaseData.Infrastructrue;
using BaseData.Infrastructrue.Inplements;
using BaseData.Infrastructrue.Interfaces;
using BaseData.ReadApi;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using NLog.Web;
using Yitter.IdGenerator;
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
// ����Swagger
builder.Services.AddSwaggerGen(o =>
{
    o.IncludeXmlComments(AppContext.BaseDirectory + "BaseData.ReadApi.xml", true);
});

//��Ȩ��֤��ʹ��JWT��
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(o =>
{
    // 从配置中获取JWT设置
    var jwtSettings = builder.Configuration.GetSection("Jwt");
    var secretByte = Encoding.UTF8.GetBytes(jwtSettings["SecretKey"]!);
    
    o.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
    {
        // ��֤������
        ValidateIssuer = true,
        // ��������Ϣ
        ValidIssuer = jwtSettings["Issuer"],

        // ��֤������
        ValidateAudience = true,
        // ������
        ValidAudience = jwtSettings["Audience"],

        // ��֤�Ƿ����
        ValidateLifetime = true,

        // ��֤˽Կ
        IssuerSigningKey = new SymmetricSecurityKey(secretByte)

    };
});

// ����Swagger
builder.Services.AddSwaggerGen(o =>
{
    // ��ʾ ��Ȩ��Ϣ
    o.AddSecurityDefinition("Bearer", new Microsoft.OpenApi.Models.OpenApiSecurityScheme()
    {
        Description = "����JWT��ȨToken��Bearer Tokenֵ",
        Name = "Authorization",
        In = Microsoft.OpenApi.Models.ParameterLocation.Header,
        Type = Microsoft.OpenApi.Models.SecuritySchemeType.ApiKey,
        BearerFormat = "JWT",
        Scheme = "Bearer"
    });
    o.AddSecurityRequirement(new Microsoft.OpenApi.Models.OpenApiSecurityRequirement
    {
        {
            new OpenApiSecurityScheme
            {
                Reference = new OpenApiReference
                {
                    Type = ReferenceType.SecurityScheme,
                    Id = "Bearer"
                }
            },
            new string[]
            {

            }
        }
    });
});

// �����н���
builder.Services.AddMediatR(R => R.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));
// ���ö���ӳ����
builder.Services.AddAutoMapper(typeof(MappingProfile));
// ����������
builder.Services.AddDbContext<Mycontext>(X => X.UseSqlServer(builder.Configuration.GetConnectionString("db")));
// ���òִ�
builder.Services.AddScoped(typeof(IBasepository<>),typeof(Basepository<>));
// ����ѩ��Id
YitIdHelper.SetIdGenerator(new IdGeneratorOptions());
// ���ô�Сд�淶
builder.Services.AddControllers().AddJsonOptions(P => P.JsonSerializerOptions.PropertyNamingPolicy = null);
// ������־��¼��
builder.Host.UseNLog();

var app = builder.Build();
// ����
app.UseCors(A => A.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

// ��Ȩ
app.UseAuthentication();

app.UseAuthorization();

app.MapControllers();

app.Run();
